/*
 *  Copyright 2010 Miao.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 *
 *  @(#)SecurityCodeProperty.java    0.2 01/17/2010
 */
package image;

import java.awt.Color;
import java.awt.Font;

/**
 * 验证码图形参数
 * @author Miao
 * @version 0.2
 * @since 0.2
 */
public class SecurityCodeProperty {

    /**
     * 验证码长度
     */
    private int codeLength;
    /**
     * 图形格式
     */
    private String formatName;
    /**
     * 字体
     */
    private String font;
    /**
     * 宽度
     */
    private int width;
    /**
     * 高度
     */
    private int height;
    /**
     * 干扰线数量
     */
    private int interferingLine;
    /**
     * 背景色
     */
    private Color backgroundColor;

    public SecurityCodeProperty() {
        codeLength = 6;
        formatName = "jpeg";
        font = "Arial";
        width = 135;
        height = 30;
        enableInterfering();
        backgroundColor = Color.LIGHT_GRAY;
    }

    /**
     * 获得验证码长度
     * @return 验证码长度
     * @since 0.2
     */
    public int getCodeLength() {
        return codeLength;
    }

    /**
     * 获得字体磅值
     * @return 字体磅值
     * @since 0.2
     */
    private int getFontSize() {
        return (height - 2) * 3 / 4;
    }

    /**
     * 获得字体
     * @return 字体
     * @since 0.2
     */
    public Font getFont() {
        return new Font(font, Font.PLAIN, getFontSize());
    }

    /**
     * 获得干扰数量
     * @return 干扰数量
     * @since 0.2
     */
    public int getInterferingLine() {
        return interferingLine;
    }

    /**
     * 获得宽度
     * @return 宽度
     * @since 0.2
     */
    public int getWidth() {
        return width;
    }

    /**
     * 获得高度
     * @return 高度
     * @since 0.2
     */
    public int getHeight() {
        return height;
    }

    /**
     * 获得背景色
     * @return 背景色
     * @since 0.2
     */
    public Color getBackgroundColor() {
        return backgroundColor;
    }

    /**
     * @return the x
     */
    public int getCodeX() {
        return width / (getCodeLength() + 1);
    }

    /**
     * @return the codeY
     */
    public int getCodeY() {
        return height - 4;
    }

    /**
     * 获得图形格式
     * @return 图形格式
     * @since 0.2
     */
    public String getFormatName() {
        return formatName;
    }

    /**
     * 设定验证码长度
     * @param codeLength 验证码长度
     * @since 0.2
     */
    public void setCodeLength(int codeLength) {
        this.codeLength = codeLength;
    }

    /**
     * 设定字体
     * @param font 字体
     * @since 0.2
     */
    public void setFont(String font) {
        this.font = font;
    }

    /**
     * 设定宽度
     * @param width 宽度
     * @since 0.2
     */
    public void setWidth(int width) {
        this.width = width;
    }

    /**
     * 设定高度
     * @param height 高度
     * @since 0.2
     */
    public void setHeight(int height) {
        this.height = height;
    }

    /**
     * 设定背景色
     * @param backgroundColor 背景色
     * @since 0.2
     */
    public void setBackgroundColor(String backgroundColor) {
        this.backgroundColor = new Color(Integer.decode(backgroundColor));
    }

    /**
     * 设定图形格式
     * @param formatName 图形格式
     */
    public void setFormatName(String formatName) {
        this.formatName = formatName;
    }

    /**
     * 关闭干扰
     * @since 0.2
     */
    public void disableInterfering() {
        interferingLine = 0;
    }

    /**
     * 开启干扰
     * @since 0.2
     */
    public void enableInterfering() {
        interferingLine = 100;
    }
}
